package yw.nmh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import yw.nmh.dicdata.dto.DataDicValDTO;
import yw.nmh.dicdata.dto.DataDicValQueryDTO;
import yw.nmh.dicdata.vo.DataDicValVO;
import yw.nmh.result.PageDTO;
import yw.nmh.result.PageVO;
import yw.nmh.result.R;
import yw.nmh.service.DicDataInfoService;

@RestController
@Slf4j
@Tag(name = "字典值管理", description = "字典值相关接口")
@RequestMapping("/dicData")
public class DicDataInfoController {

    @Resource
    private DicDataInfoService dicDataInfoService;

    @Operation(summary = "查询字典全部字段", description = "查询字典全部字段")
    @PostMapping("/listDicData")
    public R getDataList(){
        return R.OK(dicDataInfoService.list());
    }


    @Operation(summary = "查询字典单条字段", description = "查询字典单条字段")
    @PostMapping("/oneDicData")
    public R getOne(@RequestParam Integer valId){
        return R.OK(dicDataInfoService.One(valId));
    }
    @Operation(summary = "点击返回该字典下的所有字典值", description = "点击返回该字典下的所有字典值")
    @PostMapping("/listDataDicVal/{dicId}")
    public R listDataDicVal(@PathVariable Integer dicId, @RequestBody PageDTO pageDTO){
        Page<DataDicValVO> dataDicValVOPage = dicDataInfoService.listDataDicVal(dicId, pageDTO);
        return convert(dataDicValVOPage);
    }
    @Operation(summary = "根据名称状态查询", description = "根据名称状态查询")
    @PostMapping("/listByQuery")
    public R listByQuery(@RequestBody DataDicValQueryDTO dataDicValQueryDTO){
        Page<DataDicValVO> dataDicValVOPage = dicDataInfoService.listByQuery(dataDicValQueryDTO);
        return convert(dataDicValVOPage);
    }

    @Operation(summary = "添加字典值", description = "添加字典值")
    @PostMapping("/addDicData")
    public R addDicData(@RequestBody DataDicValDTO dataDicValDTO){
         return R.OK(dicDataInfoService.addDicData(dataDicValDTO)?"添加成功":"添加失败,字典中有相同值");
    }

    private R convert(Page<DataDicValVO> dataDicVal) {
        PageVO<DataDicValVO> dicValVOPageVO = new PageVO<>();
        dicValVOPageVO.setCurrent(dataDicVal.getCurrent());
        dicValVOPageVO.setSize(dataDicVal.getSize());
        dicValVOPageVO.setTotal(dataDicVal.getTotal());
        dicValVOPageVO.setPages(dataDicVal.getPages());
        dicValVOPageVO.setRecords(dataDicVal.getRecords());
        return R.OK(dicValVOPageVO);
    }

}
